package pl.mcnova.view.filters;

import java.util.List;

import javax.swing.RowFilter;

import pl.mcnova.model.dao.DeviceDAO;
import pl.mcnova.model.entity.Contractor;
import pl.mcnova.model.entity.PhoneNumber;
import pl.mcnova.model.table.ContractorTableModel;

//public class ContractorFilter extends RowFilter<ContractorTableModel, Integer> {
public class ContractorFilter extends RowFilter<Object, Object> {

	private String phoneNumber;
	private Integer deviceNumber;
	private boolean result;
	private DeviceDAO deviceDAO;
	private String status;

	public ContractorFilter(String phoneNumber, Integer deviceNumber, String status) {
		super();
		this.phoneNumber = phoneNumber;
		this.deviceNumber = deviceNumber;
		this.result = true;
		this.deviceDAO = new DeviceDAO();
		this.status = status;
	}

	@Override
	public boolean include(javax.swing.RowFilter.Entry<? extends Object, ? extends Object> entry) {

		ContractorTableModel model = (ContractorTableModel) entry.getModel();
		Contractor con = model.get((Integer) entry.getIdentifier());

		if (con == null) return false;

		if (!"".equals(phoneNumber) && !contains(con.getPhones())) return false;
		if (deviceNumber > 0) return deviceDAO.checkContractorDevice(con.getId(), deviceNumber);
		if (!"".equals(this.status) && !con.getStatus().equals(this.status)) return false;

		return true;

	}

	public boolean contains(List<PhoneNumber> list) {
		for (PhoneNumber ph : list)
			if (ph.getNumber().equals(this.phoneNumber)) return true;
		return false;
	}
}
